using System;
using System.Xml;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for Math.
	/// </summary>
	public class Math : Microsoft.Office.Interop.InfoPath.SemiTrust.IPMath
	{
		private InfoJetSoft.Service.Util.ExtXdMathFunction mathFunction = null;

		public Math( InfoJetSoft.Service.Entity.Form form )
		{
			this.mathFunction = new InfoJetSoft.Service.Util.ExtXdMathFunction( form );
		}

		public object Avg(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList pxmllistInput)
		{
			if( pxmllistInput is XMLNodeList )
			{
				double min = 0;
				bool inited = false;
				string nanValue = "";
				XmlNodeList xmlNodeList = ((XMLNodeList)pxmllistInput).XmlNodeList;
				foreach( XmlNode xmlNode in xmlNodeList )
				{
					string nodeValue = DOMUtil.getNodeValue( xmlNode );
					if( nodeValue.Length > 0 )
					{
						try
						{
							double doubleValue = System.Double.Parse( nodeValue );
							if( !inited )
							{
								min = doubleValue;
								inited = true;
							}
							else
							{
								if( min > doubleValue )
								{
									min = doubleValue;
								} 
							}
						}
						catch( Exception e )
						{
							nanValue = "NaN";
							break;
						}
					}
				}
				if( nanValue.Length > 0 )
				{
					return nanValue;
				}
				if( !inited )
				{
					return "";
				}
				else
				{
					return min;
				}
			}
			return null;
		}

		public object Max(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList pxmllistInput)
		{
			if( pxmllistInput is XMLNodeList )
			{
				double max = 0;
				bool inited = false;
				string nanValue = "";
				XmlNodeList xmlNodeList = ((XMLNodeList)pxmllistInput).XmlNodeList;
				foreach( XmlNode xmlNode in xmlNodeList )
				{
					string nodeValue = DOMUtil.getNodeValue( xmlNode );
					if( nodeValue.Length > 0 )
					{
						try
						{
							double doubleValue = System.Double.Parse( nodeValue );
							if( !inited )
							{
								max = doubleValue;
								inited = true;
							}
							else
							{
								if( max < doubleValue )
								{
									max = doubleValue;
								} 
							}
						}
						catch( Exception e )
						{
							nanValue = "NaN";
							break;
						}
					}
				}
				if( nanValue.Length > 0 )
				{
					return nanValue;
				}
				if( !inited )
				{
					return "";
				}
				else
				{
					return max;
				}
			}
			return null;
		}

		public object Min(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList pxmllistInput)
		{
			if( pxmllistInput is XMLNodeList )
			{
				double min = 0;
				bool inited = false;
				string nanValue = "";
				XmlNodeList xmlNodeList = ((XMLNodeList)pxmllistInput).XmlNodeList;
				foreach( XmlNode xmlNode in xmlNodeList )
				{
					string nodeValue = DOMUtil.getNodeValue( xmlNode );
					if( nodeValue.Length > 0 )
					{
						try
						{
							double doubleValue = System.Double.Parse( nodeValue );
							if( !inited )
							{
								min = doubleValue;
								inited = true;
							}
							else
							{
								if( min > doubleValue )
								{
									min = doubleValue;
								} 
							}
						}
						catch( Exception e )
						{
							nanValue = "NaN";
							break;
						}
					}
				}
				if( nanValue.Length > 0 )
				{
					return nanValue;
				}
				if( !inited )
				{
					return "";
				}
				else
				{
					return min;
				}
			}
			return null;
		}

		//Unsupported

		public object Nz(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList pxmllistInput)
		{
			return null;
		}

		public object Eval(Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList pxmllistContext, string bstrExpression)
		{
			return null;
		}

	}
}
